﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Common;
using Chowpoo.Util;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using Webdiyer.WebControls.Mvc;

namespace CourseSystem.Web.Controllers
{
    public class ResourcesController : BaseController
    {
        //
        // GET: /Resources/

        public ActionResult Index(byte? type, byte? minType, int? isIcon)
        {
            isIcon = isIcon.CheckInt(0);
            type = type.CheckByte(1);
            minType = minType.CheckByte(1);
            ViewData["minType"] = minType.Value;
            ViewData["isIcon"] = isIcon.Value;
            var info = CommonResourcesBLL.Get(type.Value, minType.Value);
            var viewName = ResourcesHelper.GetTypeByEn(type.Value);
            if (type == 1)
                viewName = "Index";
            if (info == null)
            {
                return View(viewName, new CommonResourcesInfo { Type = type.Value, MinType = minType.Value });
            }
            return View(viewName, info);
        }

        public ActionResult TreeUc(byte type, byte minType,int? isIcon)
        {
            ViewData["type"] = type;
            ViewData["minType"] = minType;
            isIcon = isIcon.CheckInt(0);
            ViewData["isIcon"] = isIcon.Value;
            ViewData.Model = ResourcesHelper.GetTextType(true);
            return PartialView();
        }

        public ActionResult CommonListUc(int? pid, string search, byte minType, byte type, int? isIcon)
        {
            CommonRequest.ClearCache();
            isIcon = isIcon.CheckInt(0);
            var list = CommonResourcesBLL.GetList(type, minType, search).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            if (isIcon == 0)
                return PartialView(ResourcesHelper.GetTypeByEn(type) + "ListUc", model);
            return PartialView(ResourcesHelper.GetTypeByEn(type) + "ViewListUc", model);
        }

        public ActionResult View(int id)
        {
            var info = CommonResourcesBLL.Get(id);
            if (info == null)
                return NotFound();
            return View(ResourcesHelper.GetTypeByEn(info.Type) + "View", info);
        }


    }
}
